from strategy import strategy_base


# 策略1，最简单的贪心策略
# 在持有__max_keep_stock_num只股票时
# 每__max_keep_duration天调仓一次
# 并买入__recent_day天中涨幅最大的几只股票
class GreedyStrategy(strategy_base.TradeStrategyBase):
    def __init__(self, recent_day, max_keep_duration, max_keep_stock_num):
        strategy_base.TradeStrategyBase.__init__(self, max_keep_duration, max_keep_stock_num)
        self.recent_day = recent_day

    def buy_strategy(self, **kwargs):
        time_data, now_date = kwargs["time_data"], kwargs["now_date"]
        # 股票太多了，不能买
        if len(self.stock_names) != self.max_keep_stock_num:
            stock_list = time_data.get_recent_max_increase(now_date, self.recent_day,
                                                       self.max_keep_stock_num - len(self.stock_names))
            for stock in stock_list:
                self.stock_names.append([stock, 0])
        for i in range(0, len(self.stock_names)):
            self.stock_names[i][1] += 1
        print("执行了买入操作，现在持有的股票状态：")
        print(self.stock_names)

    def sell_strategy(self, **kwargs):
        time_data = kwargs["time_data"]
        # 到了调仓日期，卖股票
        self.stock_names = list(filter(lambda x: x[1] < self.max_keep_duration, self.stock_names))
        print("执行了卖出操作，现在持有的股票状态：")
        print(self.stock_names)

